package com.xuecheng.content.controller;

import com.xuecheng.api.contant.FileManagerApi;
import com.xuecheng.api.contant.model.file.UploadTokenResult;
import com.xuecheng.common.exception.ExceptionCast;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p></p>
 *
 * .@Description:
 */
@Slf4j
@RestController
public class FileManagerController implements FileManagerApi {

    @Autowired
    private RestTemplate restTemplate;

    /*
    * #文件系统微服的请求地址
        file.service.url=http://127.0.0.1:56082/farming/generatetoken?origin=qiniu
        #文件存储空间名称
        file.service.bucket= xc138-static-imgs


        #文件存储区域的地址
        file.service.upload.region = http://up.qiniu.com
        #文件访问的cdn加速域名
        cdn.domain = r63c4tdkv.hd-bkt.clouddn.com

        file.token.type = 1
        file.token.deadline = 3600
    * */
    @Value("${file.service.url}")
    private String url;
    @Value("${file.service.bucket}")
    private String bucket;
    @Value("${file.service.upload.region}")
    private String region;
    @Value("${cdn.domain}")
    private String domain;
    @Value("${file.token.type}")
    private String type;
    @Value("${file.token.deadline}")
    private int deadline;

    @GetMapping("common/qnUploadToken")
    public UploadTokenResult generateUploadToken() {

        // 1.判断登录后的教学机构数据，如果有允许操作
        // Long companyId = SecurityUtil.getCompanyId();
        //
        // if (ObjectUtils.isEmpty(companyId)) {
        //     ExceptionCast.cast(CommonErrorCode.E_100108);
        // }



        // 2.调用文件微服务获得上传凭证

        HashMap<String, Object> requestBody = new HashMap<>();

        requestBody.put("tokenType",type);
        requestBody.put("scope",bucket);
        requestBody.put("deadline",deadline);

        // 为确保每个文件的是唯一的，需要生成一个不重复key值
        String fileKey = UUID.randomUUID().toString() + RandomStringUtils.randomAlphanumeric(16);

        requestBody.put("key",fileKey);

        // 3.封装上传凭证数据
        ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, requestBody, Map.class);

        int statusCodeValue = responseEntity.getStatusCodeValue();

        Map body = responseEntity.getBody();
        if (!(HttpStatus.OK.value() == statusCodeValue)) {
            ExceptionCast.castWithCodeAndDesc(new Integer(body.get("code").toString()),body.get("msg").toString());
        }
        Object token = body.get("result");
        UploadTokenResult tokenResult = new UploadTokenResult();
        tokenResult.setQnToken(token.toString());
        tokenResult.setTokenType(type);
        tokenResult.setDeadline(deadline);
        tokenResult.setDomain(domain);
        tokenResult.setUp_region(region);
        tokenResult.setScope(bucket);
        tokenResult.setKey(fileKey);

        return tokenResult;
    }
}
